package it.polimi.swKnights.SWIMv2.ejb.personalData;

import it.polimi.swKnights.SWIMv2.ejb.entityBeans.Address;
import it.polimi.swKnights.SWIMv2.ejb.entityBeans.RegisteredUser;
import it.polimi.swKnights.SWIMv2.ejb.searchEngine.ResultCategory;
import it.polimi.swKnights.SWIMv2.utility.DateUtility;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * Session Bean implementation class PersonalDataManager
 */
@Stateless
public class PersonalDataManager implements PersonalDataManagerLocal
{
	@PersistenceContext(unitName = "swimv2-manager")
	private EntityManager manager;

	@Override
	public List<Map<String, String>> getPersonalDataFromUser(
			List<RegisteredUser> users, ResultCategory category)
	{
		List<Map<String, String>> userDataList = new LinkedList<Map<String, String>>();
		for (RegisteredUser user : users)
		{
			Map<String, String> userData = this.getUserDataMap(user, category);
			userDataList.add(userData);
		}
		return userDataList;
	}

	@Override
	public Map<String, String> getUserDataMap(RegisteredUser user,
			ResultCategory category)
	{
		Map<String, String> userData = new LinkedHashMap<String, String>();
		if (category != null)
		{
			userData.put("category", category.toString());
		}

		userData.put("username", user.getUserName());
		userData.put("image", user.getImage());

		if (user.isPublic() || category == ResultCategory.friend
				|| category == ResultCategory.myself)
		{
			userData.put("surname", user.getSurname());
			userData.put("name", user.getName());
			userData.put("sex", "" + user.getSex());
			userData.put("email", user.geteMail());

			if (user.getBirthday() != null)
			{
				String strDate = DateUtility.getStringDate(user.getBirthday());
				userData.put("birthDay", strDate);
			}

			Address address = user.getAddress();
			if (address != null)
			{
				userData.put("city", address.getCity());
				userData.put("street", address.getStreet());
				userData.put("number", address.getNumber());
			}
		}
		return userData;
	}

	@Override
	public Map<String, String> getUserDataMap(RegisteredUser user,
			ResultCategory category, String ability)
	{
		Map<String, String> userData = this.getUserDataMap(user, category);
		userData.put("matched_ability", ability);
		return userData;
	}
}
